package com.practice.java8.stream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReduceMain {

	public static void main(String[] args) {
		reduce();
		max();
		distinct();
	}
	
	public static void reduce() {
		// concat = "ABCD"
		String concat = Stream.of("A", "B", "C", "D").reduce("", String::concat); 
		// minValue = -3.0
		double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min); 
		// sumValue = 10, with identity
		int sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);
		// sumValue = 10, without identity
		sumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();
		// concat = "ace"
		concat = Stream.of("a", "B", "c", "D", "e", "F").
				filter(x -> x.compareTo("Z") > 0).
				reduce("", String::concat);
	}
	
	public static void  max() {
		BufferedReader br = new BufferedReader(new StringReader(""));
		int longest = br.lines().
			mapToInt(String::length).
			max().
			getAsInt();
		try {
			br.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		System.out.println(longest);
	}
	
	public static void distinct() {
		BufferedReader br = new BufferedReader(new StringReader(""));
		List<String> words = br.lines().
			flatMap(line -> Stream.of(line.split(" "))).
			filter(word -> word.length() > 0).
			map(String::toLowerCase).
			distinct().
			sorted().
			collect(Collectors.toList());
		try {
			br.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		System.out.println(words);
	}

}
